package com.punai.dataanalysis.epdatakb.service.NG001;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.core.domain.entity.EcoCompany;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.bo.NG001.JtczClf;
import com.punai.dataanalysis.epdatakb.mapper.JtczTjClfMapper;
import com.punai.dataanalysis.epdatakb.mapper.JtczTjRgfMapper;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.system.mapper.EcoCompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NG001JtczClfService {

    @Autowired
    private JtczTjClfMapper clfMapper;
    @Autowired
    private EcoCompanyMapper ecoCompanyMapper;

    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {
        Map<String, Object> yearMonth = dataAnalysisVo.getYearMonth();


        List<JtczClf> resultList = new ArrayList<>();
        /* 土建 */
        List<JtczResultBo> tjwccz = clfMapper.tjwccz(yearMonth);
        Map<String, BigDecimal> tjwcczMap = tjwccz.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> tjzc = clfMapper.tjzc(yearMonth);
        Map<String, BigDecimal> tjzcMap = tjzc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> tjfc = clfMapper.tjfc(yearMonth);
        Map<String, BigDecimal> tjfcMap = tjfc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> fmmb = clfMapper.fmmb(yearMonth);
        Map<String, BigDecimal> fmmbMap = fmmb.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> zczlf = clfMapper.zczlf(yearMonth);
        Map<String, BigDecimal> zczlfMap = zczlf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        /* 安装 */
        List<JtczResultBo> azwccz = clfMapper.azwccz(yearMonth);
        Map<String, BigDecimal> azwcczMap = azwccz.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> azzc = clfMapper.azzc(yearMonth);
        Map<String, BigDecimal> azzcMap = azzc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> azfc = clfMapper.azfc(yearMonth);
        Map<String, BigDecimal> azfcMap = azfc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));


        List<EcoCompany> companyList = ecoCompanyMapper.selectList(Wrappers.<EcoCompany>lambdaQuery().eq(
                EcoCompany::getType,"T")
                .eq(EcoCompany::getStatus,"0")
        );

        if (CollUtil.isEmpty(companyList)) {
            throw new ServiceException("无统计单位!");
        }
        List<String> companyNames = companyList.stream().map(EcoCompany::getCompanyName).distinct().sorted().collect(Collectors.toList());

        for (String companyName : companyNames) {
            JtczClf clf = new JtczClf();

            clf.setDeptName(companyName);
            clf.setYearTjWccz(tjwcczMap.getOrDefault(companyName,BigDecimal.ZERO));
            clf.setTjzc(tjzcMap.getOrDefault(companyName,BigDecimal.ZERO));
            clf.setTjfc(tjfcMap.getOrDefault(companyName,BigDecimal.ZERO));
            clf.setFmmb(fmmbMap.getOrDefault(companyName,BigDecimal.ZERO));
            clf.setZczlf(zczlfMap.getOrDefault(companyName,BigDecimal.ZERO));

            clf.setYearAzWccz(azwcczMap.getOrDefault(companyName,BigDecimal.ZERO));
            clf.setAzzc(azzcMap.getOrDefault(companyName,BigDecimal.ZERO));
            clf.setAzfc(azfcMap.getOrDefault(companyName,BigDecimal.ZERO));

            clf.setCzhj(NumberUtil.add(clf.getYearTjWccz(),clf.getYearAzWccz()));
            clf.setClfhj(NumberUtil.adds(clf.getTjzc(),clf.getTjfc(),clf.getFmmb(),clf.getZczlf()
                    ,clf.getAzzc(),clf.getAzfc()));
            calcZb(clf);
            resultList.add(clf);
        }
        calcPm(resultList);
        JtczClf clfxj = new JtczClf();
        clfxj.setDeptName("土建单位小计");
        calcXj(resultList,clfxj);
        resultList.add(clfxj);
        return resultList;
    }

    private void calcXj(List<JtczClf> resultList, JtczClf clfxj) {
        BigDecimal tjwccz  = BigDecimal.ZERO;
        BigDecimal tjzc    = BigDecimal.ZERO;
        BigDecimal tjfc    = BigDecimal.ZERO;
        BigDecimal tjfmmb  = BigDecimal.ZERO;
        BigDecimal tjzczlf = BigDecimal.ZERO;
        BigDecimal azwccz  = BigDecimal.ZERO;
        BigDecimal azzc    = BigDecimal.ZERO;
        BigDecimal azfc    = BigDecimal.ZERO;
        BigDecimal czhj    = BigDecimal.ZERO;
        BigDecimal clfhj   = BigDecimal.ZERO;
        for (JtczClf clf : resultList) {
           tjwccz  = NumberUtil.add(tjwccz , clf.getYearTjWccz());
           tjzc    = NumberUtil.add(tjzc   , clf.getTjzc());
           tjfc    = NumberUtil.add(tjfc   , clf.getTjfc());
           tjfmmb  = NumberUtil.add(tjfmmb , clf.getFmmb());
           tjzczlf = NumberUtil.add(tjzczlf, clf.getZczlf());
           azwccz  = NumberUtil.add(azwccz , clf.getYearAzWccz());
           azzc    = NumberUtil.add(azzc   , clf.getAzzc());
           azfc    = NumberUtil.add(azfc   , clf.getAzfc());
           czhj    = NumberUtil.add(czhj   , clf.getCzhj());
           clfhj   = NumberUtil.add(clfhj  , clf.getClfhj());
        }

        clfxj.setYearTjWccz( tjwccz    );
        clfxj.setTjzc(       tjzc      );
        clfxj.setTjfc(       tjfc      );
        clfxj.setFmmb(       tjfmmb    );
        clfxj.setZczlf(      tjzczlf   );
        clfxj.setYearAzWccz( azwccz    );
        clfxj.setAzzc(       azzc      );
        clfxj.setAzfc(       azfc      );
        clfxj.setCzhj(       czhj      );
        clfxj.setClfhj(      clfhj     );
        calcZb(clfxj);
    }

    private void calcPm(List<JtczClf> resultList) {
        resultList.sort(Comparator.comparing(JtczClf::getTjzczb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczClf clf = resultList.get(i);
            clf.setTjzcpm(i + 1);
        }

        resultList.sort(Comparator.comparing(JtczClf::getTjfczb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczClf clf = resultList.get(i);
            clf.setTjfcpm(i + 1);
        }

        resultList.sort(Comparator.comparing(JtczClf::getFmmbzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczClf clf = resultList.get(i);
            clf.setFmmbpm(i + 1);
        }

        resultList.sort(Comparator.comparing(JtczClf::getZczlfzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczClf clf = resultList.get(i);
            clf.setZczlfpm(i + 1);
        }

        resultList.sort(Comparator.comparing(JtczClf::getAzzczb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczClf clf = resultList.get(i);
            clf.setAzzcpm(i + 1);
        }

        resultList.sort(Comparator.comparing(JtczClf::getAzfczb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczClf clf = resultList.get(i);
            clf.setAzfcpm(i + 1);
        }

        resultList.sort(Comparator.comparing(JtczClf::getZb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczClf clf = resultList.get(i);
            clf.setPm(i + 1);
        }
    }

    private void calcZb(JtczClf clf) {
        BigDecimal yearTjWccz = clf.getYearTjWccz();
        BigDecimal tjzc = clf.getTjzc();
        BigDecimal tjfc = clf.getTjfc();
        BigDecimal fmmb = clf.getFmmb();
        BigDecimal zczlf = clf.getZczlf();
        clf.setTjzczb(NumberUtil.divideUseProportion(tjzc,2,yearTjWccz));
        clf.setTjfczb(NumberUtil.divideUseProportion(tjfc,2,yearTjWccz));
        clf.setFmmbzb(NumberUtil.divideUseProportion(fmmb,2,yearTjWccz));
        clf.setZczlfzb(NumberUtil.divideUseProportion(zczlf,2,yearTjWccz));

        BigDecimal yearAzWccz = clf.getYearAzWccz();
        BigDecimal azzc = clf.getAzzc();
        BigDecimal azfc = clf.getAzfc();
        clf.setAzzczb(NumberUtil.divideUseProportion(azzc,2,yearAzWccz));
        clf.setAzfczb(NumberUtil.divideUseProportion(azfc,2,yearAzWccz));

        BigDecimal czhj = clf.getCzhj();
        BigDecimal clfhj = clf.getClfhj();
        clf.setZb(NumberUtil.divideUseProportion(clfhj,2,czhj));
    }


}
